<!--
 * @Author: Rain
 * @Date: 2023/11/02 21:55:03
 * @LastEditTime: 2023/12/04 22:45:42
 * @Description: Do Something
-->
<template>
  <div class="app-container w-full h-full overflow-hidden">
    <!--工具栏-->
    <div class="head-container w-full h-full flex flex-col">
      <div class="w-full flex items-center justify-end search pb-4">
        <!-- 搜索 -->
        <label class="el-form-item-label">标题</label>
        <el-input
          v-model="query.title"
          clearable
          placeholder="标题"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">见证类型</label>
        <el-select v-model="query.witnessType" placeholder="请选择" clearable>
          <el-option
            v-for="item in [
              { value: 1, label: '课程感悟' },
              { value: 2, label: '活力打call' },
              { value: 3, label: '课程集锦' },
            ]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
        <crudOperation :permission="permission" />
      </div>
      <div class="w-full flex-1 overflow-hidden">
        <el-table
          ref="table"
          v-loading="crud.loading"
          :data="crud.data"
          size="small"
          style="width: 100%"
          height="100%"
          @selection-change="crud.selectionChangeHandler"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="title" label="标题" />
          <el-table-column prop="witnessType" label="见证类型">
            <template slot-scope="{ row }">
              {{
                row.witnessType === 1
                  ? "课程感悟"
                  : row.witnessType === 2
                    ? "活力打call"
                    : "课程集锦"
              }}
            </template>
          </el-table-column>
          <el-table-column
            v-if="
              checkPer(['admin', 'studentWitnessConfig:edit', 'studentWitnessConfig:del'])
            "
            label="操作"
            width="150px"
            align="center"
          >
            <template slot-scope="scope">
              <udOperation :data="scope.row" :permission="permission" />
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页组件-->
      <pagination />
    </div>
    <!--表单组件-->
    <el-dialog
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="700px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
        <el-form-item
          label="标题"
          prop="title"
          :rules="[{ required: true, message: '请输入标题', trigger: 'blur' }]"
        >
          <el-input v-model="form.title" class="w-full" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item
          label="见证类型"
          prop="witnessType"
          :rules="[{ required: true, message: '请选择见证类型', trigger: 'blur' }]"
        >
          <el-select v-model="form.witnessType" placeholder="请选择" class="w-full">
            <el-option
              v-for="item in [
                { value: 1, label: '课程感悟' },
                { value: 2, label: '活力打call' },
                { value: 3, label: '课程集锦' },
              ]"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="form.witnessType === 1"
          class="w-full"
          label="详细内容"
          prop="itemContent"
          :rules="[{ required: true, message: '请输入详细内容', trigger: 'blur' }]"
        >
          <Editor ref="editor" :content="form.itemContent" />
        </el-form-item>
        <el-form-item
          v-if="form.witnessType === 2"
          class="w-full"
          label="详细内容"
          prop="itemContent"
          :rules="[{ required: true, message: '请输入详细内容', trigger: 'blur' }]"
        >
          <UploadFile ref="uploadFile" :files="fileList" type="media" />
        </el-form-item>
        <el-form-item
          v-if="form.witnessType === 3"
          class="w-full"
          label="详细内容"
          prop="itemContent"
          :rules="[{ required: true, message: '请输入详细内容', trigger: 'blur' }]"
        >
          <UploadImgs ref="uploadImg" :files="fileList" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="default" @click="crud.cancelCU">取消</el-button>
        <el-button
          :loading="crud.status.cu === 2"
          type="primary"
          @click="submitAction"
        >确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import crudStudentWitnessConfig, { getConfigDetail } from '@/api/studentWitnessConfig'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import Editor from '@/components/Editor'
import UploadFile from '@/components/uploadFile/multiple'
import UploadImgs from '@/components/uploadImage/multiple'

const defaultForm = {
  witnessId: null,
  witnessType: null,
  title: null,
  witnessStyle: null,
  itemContent: '',
  createTime: null,
  createby: null,
  updateTime: null,
  updateby: null
}
export default {
  name: 'StudentWitnessConfig',
  components: {
    pagination,
    crudOperation,
    rrOperation,
    udOperation,
    Editor,
    UploadFile,
    UploadImgs
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '学员见证配置',
      url: 'api/studentWitnessConfig',
      idField: 'witnessId',
      sort: 'witnessId,desc',
      crudMethod: { ...crudStudentWitnessConfig }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'studentWitnessConfig:add'],
        edit: ['admin', 'studentWitnessConfig:edit'],
        del: ['admin', 'studentWitnessConfig:del']
      },
      rules: {},
      queryTypeOptions: [
        {
          key: 'witnessType',
          display_name: '见证类型'
        },
        { key: 'title', display_name: '标题' }
      ],
      fileList: []
    }
  },
  watch: {
    'form.witnessType'(val) {
      this.form.witnessStyle = val === 1 ? 1 : val === 2 ? 3 : 2
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterAddCancel]() {
      this.$refs['form'].clearValidate()
      this.$refs['form'].resetFields()
    },
    [CRUD.HOOK.afterToEdit]({ form: { witnessId, witnessType }}) {
      getConfigDetail({ witnessId, size: 10000 }).then((res) => {
        const { content } = res
        this.fileList = content.map((item) => {
          const { itemContent } = item
          item['filePath'] = itemContent
          return item
        })
      })
    },
    submitAction() {
      if (this.form.witnessType === 2) {
        this.$refs['uploadFile'].handleUpload().then((result) => {
          this.form['itemList'] = result
          this.form.itemContent = result.join(',')
          this.crud.submitCU()
        })
      } else if (this.form.witnessType === 3) {
        this.$refs['uploadImg'].handleUpload().then((result) => {
          this.form['itemList'] = result
          this.form.itemContent = result.join(',')
          this.crud.submitCU()
        })
      } else {
        this.crud.submitCU()
      }
    },
    [CRUD.HOOK.beforeValidateCU]() {
      if (this.form.witnessType === 1) {
        const content = this.$refs['editor'].getContent()
        this.form.itemContent = content
        this.form['itemList'] = [content]
      } else {
        if (this.fileList.length > 0) {
          this.form.itemContent = this.fileList.map((item) => item.filePath).join(',')
        }
      }
    }
  }
}
</script>

<style scoped></style>
